﻿using System;
using System.Collections.Generic;
using System.Text;
using AutoMapper;
using FH.Application.NewsApp.Dtos;
using FH.Core.Domain.IRepositories;
using FH.Core.Entities;

namespace FH.Application.NewsApp
{
    public class NewsAppService : INewsAppService
    {
        private readonly INewsRepository _repository;
        public NewsAppService(INewsRepository newsRepository)
        {
            _repository = newsRepository;
        }
        public void Delete(Guid id)
        {
            _repository.Delete(id);
        }

        public void DeleteBatch(List<Guid> ids)
        {
            _repository.Delete(it => ids.Contains(it.Id));
        }

        public NewsDto Get(Guid id)
        {
            return Mapper.Map<NewsDto>(_repository.Get(id));
        }

        public List<NewsDto> GetNewsPage(int startPage, int pageSize, out int rowCount)
        {
            return Mapper.Map<List<NewsDto>>(_repository.LoadPageList(startPage, pageSize, out rowCount, null, it => it.SortId));
        }

        public NewsDto InsertOrUpdate(NewsDto dto)
        {
            if (Get(dto.Id) != null)
                _repository.Delete(dto.Id);
            var user = _repository.InsertOrUpdate(Mapper.Map<News>(dto));
            return Mapper.Map<NewsDto>(user);
        }
    }
}
